/*
 * @Author: 颜锦忠
 * @Date: 2021-04-06 09:49:40
 */
import { VuexModule, Module, Mutation, getModule, Action } from 'vuex-module-decorators';

import store from '../index';
import { UserLogout } from '@/axios/modules/user';
import WebStorage from 'utils/cache';
@Module({
  dynamic: true,
  namespaced: true,
  name: 'user',
  store,
})
class UserStore extends VuexModule {
  public token = WebStorage.get('token');
  public userInfo = WebStorage.get('userInfo');

  @Mutation
  private commitUserToken(token: string) {
    this.token = token;
  }
  @Mutation
  private commitUserInfo(data: any) {
    this.userInfo = data;
  }

  @Action
  public actionGetUserToken(token: string) {
    this.commitUserToken(token);
  }
  @Action
  public actionGetUserInfo(data: any) {
    this.commitUserInfo(data);
  }

  @Action
  public async actionUserLogout() {
    await UserLogout();
    this.commitUserToken('');
    this.commitUserInfo({});
    WebStorage.clear();
    window.location.reload();
  }
}

export default getModule(UserStore);
